/*
 * Minutia_container.cpp
 *
 *  Created on: 2011-08-10
 *      Author: czareq
 */

#include "Minutia_container.h"

using namespace std;

// comparison minutia
bool x_compare(Minutia first, Minutia second)
{
	double first_x = first.get_x_coordinate();
	double second_x = second.get_x_coordinate();

	return first_x < second_x;
}

// comparison minutia
bool y_compare(Minutia first, Minutia second)
{
	double first_y = first.get_y_coordinate();
	double second_y = second.get_y_coordinate();

	return first_y < second_y;
}
Minutia_container::Minutia_container()
{
	// TODO Auto-generated constructor stub

}

Minutia_container::~Minutia_container()
{
	// TODO Auto-generated destructor stub
}

bool Minutia_container::load(string p_path)
{

	x_sorted_minutia.clear();
	y_sorted_minutia.clear();

	ifstream plik;
	string line;
	string x_str, y_str, angle_str, quality_str;
	plik.open(p_path.c_str(), ifstream::in);

	if (plik.is_open() != true){
		return false;
	}


	while (!plik.eof())
	{
		getline(plik, line);
		if (line.size() == 0)
		{
			continue;
		}
		istringstream iss(line);
		iss >> x_str;
		iss >> y_str;
		iss >> angle_str;
		iss >> quality_str;

		int x = atoi(x_str.c_str());
		int y = atoi(y_str.c_str());
		int angle = atoi(angle_str.c_str());
		int quality = atoi(quality_str.c_str());

		Minutia minutia;

		minutia.set_x_coordinate(x);
		minutia.set_y_coordinate(y);
		minutia.set_angle(angle);
		minutia.set_quality(quality);

		x_sorted_minutia.push_back(minutia);
		y_sorted_minutia.push_back(minutia);
	}
	plik.close();
	x_sorted_minutia.sort(x_compare);
	y_sorted_minutia.sort(y_compare);
	return true;
}

Minutia Minutia_container::get_minutia(int p_x, int p_y)
{

	list<Minutia>::iterator it;
	for (it = x_sorted_minutia.begin(); it != x_sorted_minutia.end(); it++)
	{
		if ((*it).get_x_coordinate() == p_x and (*it).get_y_coordinate() == p_y)
		{
			return *it;
		}
	}
	return Minutia();
}

bool Minutia_container::show()
{ //debug method

	cout << "x_sorted_minutia" << endl;
	list<Minutia>::iterator it;
	vector<double> all_distances;
	vector<double>::iterator dist_it;
	vector<double> all_directions;
	vector<double>::iterator dir_it;

	for (it = x_sorted_minutia.begin(); it != x_sorted_minutia.end(); it++)
	{
		cout << "Minutia: ";
		cout << (*it).get_x_coordinate() << " " << (*it).get_y_coordinate() << " " << (*it).get_angle() << " "
				<< (*it).get_quality() << endl;
	}
	return true;
}

list<Minutia> Minutia_container::all_minutia()
{
	return x_sorted_minutia;
}

void Minutia_container::add(Minutia minutia)
{
	x_sorted_minutia.push_back(minutia);
	y_sorted_minutia.push_back(minutia);
//	x_sorted_minutia.sort(x_compare);
//	y_sorted_minutia.sort(y_compare);
}
